package com.vip.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vip.commonutils.R;
import com.vip.eduservice.entity.EduTeacher;
import com.vip.eduservice.entity.vo.TeacherQuery;
import com.vip.eduservice.service.EduTeacherService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.PermitAll;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author hvip
 * @since 2020-08-19
 */
@RestController
//@CrossOrigin  //解决跨域
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {

    // 把service注入
    @Autowired
    private EduTeacherService teacherService;

    // 1.查询所有数据
    // rest风格
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public R findAllTeacher() {
        // 调用service方法
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("items", list);
    }

    // 2.逻辑删除 PathVariable 通过路径传递id
    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("{id}")
    public R removeTeacher(@PathVariable String id) {

        boolean flag = teacherService.removeById(id);
        if (flag)
            return R.ok();
        else
            return R.error();
    }

    // 分页查询
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current, @PathVariable long limit) {

        // 创建page对象
        Page<EduTeacher> page = new Page<>(current, limit);

        // 调用方法实现分页
        teacherService.page(page, null);

        long total = page.getTotal();// 总记录数
        List<EduTeacher> records = page.getRecords(); // 数据list集合

        // 两种方法都可以
//        Map map = new HashMap();
//        map.put("total", total);
//        map.put("rows", records);
//        return R.ok().data(map);

        return R.ok().data("total",total).data("rows", records);
    }

    // 条件查询带分页
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current, @PathVariable long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery) {
        // 创建对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        // 构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        if (!StringUtils.isEmpty(name)) {
            // 构建条件
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level) ) {
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end);
        }

        // 排序
        wrapper.orderByDesc("gmt_create");
        // wapper
        teacherService.page(pageTeacher, wrapper);

        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();

        return R.ok().data("total",total).data("rows", records);
    }

    // 添加讲师接口
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = teacherService.save(eduTeacher);
        if (save)
            return R.ok();
        else
            return R.error();
    }

    // 根据id查询
    @GetMapping("getTeacher/{id}")
    public R getById(@PathVariable String id){
        EduTeacher teacher = teacherService.getById(id);
        return R.ok().data("teacher", teacher);
    }

    // 根据id修改
    @PostMapping("updateTeacher")
    public R updateById(@RequestBody EduTeacher teacher){
        boolean flag = teacherService.updateById(teacher);
        if (flag)
            return R.ok();
        else
            return R.error();
    }
}

